// ==================================================================
// 描 述：
// 作 者：王征:D
// 创建时间：2019/06/04 16:37:35
// ==============================================================
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public class HeroShowUpState : Yunchang.FsmStateBase<HeroBehaviourState>
    {
        private Action endCall;
        private float curvePlayTime = 0.5f;
        private float curveTimmer = 0;
        private float curScale = 1;
        private CommonAnimCurve jumpCurve;
        private bool needPlayEnterAnim = false;

        private HeroBehaviourCtr ctr;
        public HeroShowUpState(HeroBehaviourState stateType, HeroBehaviourCtr controller) : base(stateType, controller)
        {
            ctr = controller;
        }

        public override void Enter()
        {
            curveTimmer = 0;
            jumpCurve = AutoChessFieldViewCtr.Ins.SceneConfig.heroInGroundJumpCurve;
            endCall = ctr.showUpData.callEnd;
            //ctr.heroRenderer.PlayAnimation(AniName.RUSH);
            needPlayEnterAnim = true;
            curScale = ctr.heroRenderer.heroUnit.MapGrid.GridType == GridType.ShopGrid ? 0.2f : 1;
        }

        public override void Update()
        {
            if (needPlayEnterAnim)
            {
                if (ctr.heroRenderer == null || !ctr.heroRenderer.IsCanPlayAnim())
                    return;
                needPlayEnterAnim = false;
                ctr.heroRenderer.PlayAnimation(AniName.RUSH);
            }

            if (curveTimmer < curvePlayTime)
            {
                curveTimmer += Time.unscaledDeltaTime;
                float progress = curveTimmer / curvePlayTime;
                ctr.heroRenderer.ModelParent.transform.localPosition = jumpCurve.EvaluatePosition(progress) * curScale;
            }

            if (curveTimmer >= curvePlayTime)
            {
                if (endCall != null)
                {
                    endCall();
                    endCall = null;
                }
                ctr.showData = new ShowPackage(AniName.STRONG, null);
                ctr.SwitchState(HeroBehaviourState.Show);
            }
        }

        public override void Leave()
        {
            endCall = null;
            ctr.heroRenderer.SetAnimSpeed(1);
            ctr.heroRenderer.ModelParent.transform.localPosition = Vector3.zero;
        }
    }
}
